package com.laihui.activity;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.util.StringUtils;
import com.laihui.Constants;
import com.laihui.MyApplication;
import com.laihui.activity.R;
import com.laihui.base.BaseActivity;
import com.laihui.entity.RetMap;
import com.laihui.model.URL;
import com.laihui.utils.ToastUtil;
import com.laihui.view.TopBar;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 修改登录密码第一页
 * 跳转到setLoginPasswordTwo
 */
public class ChangeLoginPasswordActivity extends BaseActivity {

    @BindView(R.id.topbar)//topbar
    TopBar topbar;
    @BindView(R.id.et_old_password)//旧的登录密码
    EditText etOldPassword;
    @BindView(R.id.btn_next)//下一步按钮
            Button btnNext;

    @BindView(R.id.et_new_password)//新的登录密码
            EditText etNewPassword;
    @BindView(R.id.et_new_password_again)//再次输入新的登录密码
            EditText etNewPasswordAgain;
    private Activity mActivity;
    private MyApplication application;
    private String oldPassword = "";
    private static String Tag = "SetLoginPasswordActivity";
    @Override
    public void init() {
        mActivity = ChangeLoginPasswordActivity.this;
        application = (MyApplication) getApplication();
        int whiteColor;

        topbar.setOnTopbarClickListener(new TopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });
    }
    private void oldPassword(){
        String oldPassword = etOldPassword.getText().toString();
        if (oldPassword.isEmpty()){
            ToastUtil.showToast(mActivity,"请输入旧的登录密码");
            return;
        }
        if (oldPassword.length()<6||oldPassword.length()>16){
            ToastUtil.showToast(mActivity,"请输入正确的登录密码");
            return;
        }
        setLoginPwd(oldPassword);
    }
    private void setLoginPwd(String oldPassword){
        String newPwd = etNewPassword.getText().toString();
        String newPwdAgain = etNewPasswordAgain.getText().toString();
        //密码为数字和字母组合的正则表达式
        Pattern z1_ = Pattern.compile("^(?=.*?[a-z])(?=.*?[0-9])[a-zA-Z0-9_]{6,16}$");
        //判断是否都成立，都包含返回true
        boolean pwd = z1_.matcher(newPwd).matches();
        boolean pwdAgain = z1_.matcher(newPwdAgain).matches();
        if(newPwd.isEmpty()){
            ToastUtil.showToast(mActivity,"请输入新的登录密码");
            return;
        }
        if(newPwdAgain.isEmpty()){
            ToastUtil.showToast(mActivity,"请输入新的登录密码");
            return;
        }
        if (newPwd.length()<6||newPwd.length()>16){
            ToastUtil.showToast(mActivity,"请输入正确的登录密码");
            return;
        }
        if (newPwdAgain.length()<6||newPwdAgain.length()>16){
            ToastUtil.showToast(mActivity,"请输入正确的登录密码");
            return;
        }
        if (!newPwdAgain.equals(newPwd)){
            ToastUtil.showToast(mActivity, "两次密码输入不一致");
            return;
        }

        if(StringUtils.isEmpty(oldPassword)){
            ToastUtil.showToast(mActivity, "请输入密码");
            return;
        }
        HashMap map = new HashMap();
        map.put("oldpass",oldPassword);
        map.put("pass1",newPwd);
        map.put("pass1",newPwdAgain);
        application.apiService.passchange(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RetMap>() {
                    @Override
                    public void onCompleted() {

                    }
                    @Override
                    public void onError(Throwable e) {
                    }
                    @Override
                    public void onNext(RetMap retMap) {
                       if(retMap.status.equals("1")){
                           ToastUtil.showToast(mActivity, "修改密码成功,请重新登录");
                           application.clearLoginParams();
                           Intent intent = new Intent("android.action.user.exit");
                           sendBroadcast(intent);
                           startActivity(new Intent(mActivity, LoginActivity.class));
                           finish();
                       }else{
                           ToastUtil.showToast(ChangeLoginPasswordActivity.this,retMap.message);
                       }
                    }
                });
    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_set_password_new;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }



    @OnClick(R.id.btn_next)
    public void onClicked(View view) {
        switch (view.getId()){
            case R.id.btn_next://点击下一步跳转到下个页面
                oldPassword();
                break;
        }
    }
}
